<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <h1 class="tar">距离2021年9月1日10:00:00</h1>
  <h1 class="last">还剩</h1>
  <script>
    var oTar = document.querySelector(".tar");
    var oText = document.querySelector(".last");
    var targetTime = new Date("2021-8-1 8:00:00");
    var targetTimeStamp = targetTime.getTime();
    setInterval(function(){
      var nowTime = new Date();
      var nowTimeStamp = nowTime.getTime();
      var subTime = targetTimeStamp-nowTimeStamp;
      var subObj = fun(subTime);
      oText.innerHTML = "还剩"+ subObj.day + "天" + subObj.hour + "小时" + subObj.minute + "分钟" + subObj.second + "秒"
    },1000)
    function fun(time){
      var d = Math.floor(time/(24*60*60*1000));
      var ds = time%(24*60*60*1000);
      var h = Math.floor(ds/(60*60*1000));
      var hs = ds%(60*60*1000);
      var m = Math.floor(hs/(60*1000));
      var ms = hs%(60*1000);
      var s = Math.floor(ms/1000);
      return{
        day:d,
        hour:h,
        minute:m,
        second:s
      }
    }    
    function timeTrans(t){
      var y = t.getFullYear();
      var m = t.getMonth();
      var d = t.getDate();
      var h = t.getHours();
      var min = t.getMinutes();
      var s = t.getSeconds();
      return {
        year:y,
        month:m+1,
        date:d,
        hours:h,
        minutes:min,
        seconds:s
      }
    }
    var obj = timeTrans(targetTime)
    oTar.innerHTML = "距离"+obj.year+"年"+obj.month+"月"+obj.date+"日"+obj.hours+"小时"+obj.minutes+"分钟"+obj.seconds+"秒";
  </script>
</body>
</html>